package com.wen.mall.service.impl;

import com.wen.mall.consts.Mallconst;
import com.wen.mall.dao.CategoryDao;
import com.wen.mall.pojo.Category;
import com.wen.mall.service.CategoryService;
import com.wen.mall.vo.CategoryVo;
import com.wen.mall.vo.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryDao categoryDao;

    /**
     * 查询所有类别
     *
     * @return
     */
    @Override
    public ResponseVo<List<CategoryVo>> findAll() {
        List<Category> all = categoryDao.findAll();
        List<CategoryVo> collect = all.stream()
                .filter(e -> e.getParentId().equals(Mallconst.ROOT_PARENT_ID))
                .map(this::category2CategoryVo)
                .sorted(Comparator.comparing(CategoryVo::getSortOrder).reversed())
                .collect(Collectors.toList());
        findSubCategory(collect, all);
        log.info("collect=>" + collect);
        return ResponseVo.success(collect);
    }


    /**
     * 根据名字查询类别
     *
     * @param name
     * @return
     */
    @Override
    public ResponseVo<CategoryVo> findByName(String name) {
        Category byName = categoryDao.findByName(name);
        CategoryVo categoryVo = new CategoryVo();
        BeanUtils.copyProperties(byName, categoryVo);

        return ResponseVo.success(categoryVo);
    }

    @Override
    public void findSubcategoryId(Integer id, Set<Integer> resultSet) {
        List<Category> list = categoryDao.findAll();
        findSubcategoryId(id, resultSet, list);
    }

    private void findSubcategoryId(Integer id, Set<Integer> resultSet, List<Category> list) {

        for (Category category : list) {
            if (id.equals(category.getParentId())) {
                resultSet.add(category.getId());
                findSubcategoryId(category.getId(), resultSet, list);
            }
        }
    }

    private void findSubCategory(List<CategoryVo> categoryVoList, List<Category> categoryList) {

        for (CategoryVo categoryVo : categoryVoList) {

            List<CategoryVo> categoryVos = new ArrayList<>();
            for (Category category : categoryList) {
                if (categoryVo.getId().equals(category.getParentId())) {
                    categoryVos.add(category2CategoryVo(category));
                }
            }
            categoryVos.sort(Comparator.comparing(CategoryVo::getSortOrder).reversed());
            categoryVo.setSubCategories(categoryVos);
            findSubCategory(categoryVos, categoryList);
        }
    }

    private CategoryVo category2CategoryVo(Category category) {
        CategoryVo categoryVo = new CategoryVo();
        BeanUtils.copyProperties(category, categoryVo);
        return categoryVo;
    }
}
